#include "globalmousepath.h"
#include <QDebug>
#include <QCoreApplication>
#include <X11/Xlib.h>
#include <stdio.h>

GlobalMousePath::GlobalMousePath()
{
    onOff = false;
}

GlobalMousePath::~GlobalMousePath()
{
}

void GlobalMousePath::setCursorOn()
{
    onOff = true;
}

void GlobalMousePath::setCursorOff()
{
    onOff = false;
}

void GlobalMousePath::mousePressed()
{
    Display* display;
    display = XOpenDisplay( NULL );
    Window root = DefaultRootWindow( display);
    Window root_return, child_return;
    int x, y;
    int win_x, win_y;
    unsigned int mask;

    while( onOff )
    {
        QCoreApplication::processEvents( QEventLoop::AllEvents );
        XQueryPointer(display, root, &root_return, &child_return, &x, &y, &win_x, &win_y, &mask);

        if ((m_oldx != x) || (m_oldy != y))
        {
//            printf("Mouse dragged. Current position: (%d, %d)\n", x, y);
            emit mousemove( x, y);

            m_oldx = x;
            m_oldy = y;
        }

        fflush(stdout);
    }
    XCloseDisplay( display );
}
